<?php

namespace app\admin\controller\paotui;

use addons\paotui\library\Convert;
use addons\paotui\Paotui;
use addons\paotui\service\Config;
use app\admin\model\paotui\PostBeatSet;
use app\admin\model\paotui\PostDispatchSet;
use app\common\controller\Backend;

/**
 * 配置管理
 *
 * @icon fa fa-circle-o
 */
class SocketConfig extends Backend
{
    
    /**
     * Distanceprice模型对象
     * @var \app\admin\model\paotui\Brokeragescale
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();

    }

    public function index()
    {
        if($this->request->isPost()){


            $params = $this->request->post("row/a");

            //派单抢单模式
            $confifModel = new \app\admin\model\paotui\Config();
            $confifModel->where(['name'=>'dispatch_mode'])->update(['value'=>intval($params['dispatch_mode'])]);

            //抢单语音提醒
            $confifModel->where(['name'=>'beat_mp3'])->update(['value'=>cdnurl($params['beat_mp3'],true)]);
            $confifModel->where(['name'=>'beatmp3_1'])->update(['value'=>cdnurl($params['beatmp3_1'],true)]);
            $confifModel->where(['name'=>'beatmp3_2'])->update(['value'=>cdnurl($params['beatmp3_2'],true)]);
            $confifModel->where(['name'=>'beatmp3_3'])->update(['value'=>cdnurl($params['beatmp3_3'],true)]);
            $confifModel->where(['name'=>'beatmp3_4'])->update(['value'=>cdnurl($params['beatmp3_4'],true)]);
            $confifModel->where(['name'=>'beatmp3_5'])->update(['value'=>cdnurl($params['beatmp3_5'],true)]);

            //手动派单提醒
            $confifModel->where(['name'=>'hand_dispatch_mp3'])->update(['value'=>cdnurl($params['hand_dispatch_mp3'],true)]);

            //系统派单提
            $confifModel->where(['name'=>'auto_dispatch_mp3'])->update(['value'=>cdnurl($params['auto_dispatch_mp3'],true)]);

            //人工派单(x)秒后算拒接
            $confifModel->where(['name'=>'hand_dispatch_delay'])->update(['value'=>$params['hand_dispatch_delay']]);

            //取消订单语音提醒
            $confifModel->where(['name'=>'cancel_mp3'])->update(['value'=>cdnurl($params['cancel_mp3'],true)]);


            //新订单延迟（X）秒加入抢单广场
            $confifModel->where(['name'=>'new_post_second'])->update(['value'=>$params['new_post_second']]);

            $model = new PostBeatSet();
            $model->where('id',">","0")->delete();
            if(!empty($params['beat_config'])){
                $level = 1;
                foreach ($params['beat_config'] as $k=>$v){
                    $model = new PostBeatSet();
                    $model->save(['level'=>$level,'distance'=>$v['distance'],'num'=>$v['num'],'seconds'=>$v['seconds']]);
                    $level++;
                }
            }

            $model = new PostDispatchSet();
            $model->where('id',">","0")->delete();
            if(!empty($params['dispatch_config'])){
                $level = 1;
                foreach ($params['dispatch_config'] as $k=>$v){
                    $model = new PostDispatchSet();
                    $model->save(['level'=>$level,'distance'=>$v['distance'],'seconds'=>$v['seconds']]);
                    $level++;
                }
            }
            $this->success("请求成功");
        }

        $model = new PostBeatSet();
        $setList = collection($model->select())->toArray();
        $beatSet=[];
        foreach ($setList as $k=>$v){
            $beatSet[] = ['index'=>$v['level'],'distance'=>$v['distance'],'num'=>$v['num'],'seconds'=>$v['seconds']];
        }

        $this->assign('beatSet',json_encode($beatSet));

        $model = new PostDispatchSet();
        $setList = collection($model->select())->toArray();
        $dispatchSet=[];
        foreach ($setList as $k=>$v){
            $dispatchSet[] = ['index'=>$v['level'],'distance'=>$v['distance'],'seconds'=>$v['seconds']];
        }

        $this->assign('dispatchSet',json_encode($dispatchSet));


        $configModel = new \app\admin\model\paotui\Config();
        $paotuiConfig = $configModel->where(['name'=>['in',['dispatch_mode','cancel_mp3','hand_dispatch_delay','beat_mp3','hand_dispatch_mp3','auto_dispatch_mp3','new_post_second','beatmp3_1','beatmp3_2','beatmp3_3','beatmp3_4','beatmp3_5']]])->column('name,value');
        $this->assign("paotuiConfig",$paotuiConfig);

        return $this->fetch();
    }
}
